package com.qztc.news_end_customer.controller;

import com.qztc.news_end_customer.config.GlobalConfig;
import com.qztc.news_end_dto.dto.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/thenews/admin")
public class AdminControllerByRestTemplate extends GlobalConfig {
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/login")
    public String login(@RequestBody Admin admin) {
        Map<String,Object> params = new HashMap<>();
        params.put("adminname",admin.getAdminname());
        params.put("adminpwd",admin.getAdminpwd());
        return restTemplate.postForObject(SEVICE_URL+"admin/login",params,String.class);
    }

    @PostMapping("/add")
    public String add(@RequestBody Admin admin) {
        Map<String,Object> params = new HashMap<>();
        params.put("adminname",admin.getAdminname());
        params.put("adminpwd",admin.getAdminpwd());
        return restTemplate.postForObject(SEVICE_URL+"admin/add",params,String.class);
    }

    @DeleteMapping("/delete")
    public String delete(@RequestBody Admin admin) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Admin> httpEntity = new HttpEntity<>(admin,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"admin/delete", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @GetMapping("/get")
    public String get() {
        return restTemplate.getForObject(SEVICE_URL+"admin/get",String.class);
    }

    @PutMapping("/update")
    public String update(@RequestBody Admin admin) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Admin> httpEntity = new HttpEntity<>(admin,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"admin/update",HttpMethod.PUT,httpEntity,String.class);
        return result.getBody();
    }
}
